//
//  CZTabBarController.m
//  QQ空间
//
//  Created by zhangping on 16/1/16.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "CZTabBarController.h"
#import "CZAboutMeViewController.h"
#import "CZHomeViewController.h"
#import "CZMoreViewController.h"
#import "CZMyFriendsViewController.h"
#import "CZZoneViewController.h"
#import "CZMoodViewController.h"

@interface CZTabBarController ()

/// 撰写按钮
@property (nonatomic, strong) UIButton *composeButton;

@end

@implementation CZTabBarController

/// tabBarItem的数量
const int TabBarItemCount = 5;

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加动态控制器
    [self addChildViewController:[[CZHomeViewController alloc] init] title:@"动态" imageName:@"tabbar_icon_auth"];
    
    // 添加与我相关控制器
    [self addChildViewController:[[CZAboutMeViewController alloc] init] title:@"与我相关" imageName:@"tabbar_icon_at"];
    
    // 添加普通控制器,占用位置
    [self addChildViewController:[[UIViewController alloc] init]];
    
    // 添加我的空间控制器
    [self addChildViewController:[[CZAboutMeViewController alloc] init] title:@"我的空间" imageName:@"tabbar_icon_space"];
    
    // 添加发现控制器
    [self addChildViewController:[[CZMoreViewController alloc] init] title:@"发现" imageName:@"tabbar_icon_more"];
}

/// 注意:不能在这里设置加号按钮的frame,因为这个时候获取的frame是不对的
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    
//    [self setupComposeButton];
//}

/// 控制器的view将要布局子空间
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self setupComposeButton];
}

/// 设置加号按钮
- (void)setupComposeButton {
    // 添加加号按钮
    [self.tabBar addSubview:self.composeButton];
    
    // 设置加号按钮frame
    CGFloat width = self.tabBar.frame.size.width / TabBarItemCount;
    self.composeButton.frame = CGRectMake(2 * width - 2, 0, width + 4, self.tabBar.frame.size.height);
}

/**
 *  添加tabbar view controller 的子控制器
 *  @param controller 子控制器
 *  @param title      子控制器的标题
 *  @param imageName  子控制器的图片名称
 */
- (void)addChildViewController:(UIViewController *)controller title:(NSString *)title imageName:(NSString *)imageName {
    
    // 设置
    controller.title = title;
    controller.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 拼接高亮图片的名称
    NSString *selectedImageName = [NSString stringWithFormat:@"%@_click", imageName];
    
    // 设置高亮图片, 并且图片不需要系统的渲染,使用图片本身的颜色
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置高亮时的文字颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateSelected];
    
    // 添加动态控制器
    [self addChildViewController:[[UINavigationController alloc] initWithRootViewController:controller]];
}

#pragma mark - 点击事件
- (void)composeButtonClick {
    CZLog(@"加号按钮被点击");
    
    // TODO: 弹出写说说
    CZMoodViewController *moodVC = [[CZMoodViewController alloc] init];
    moodVC.title = @"写说说";
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:moodVC];
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 懒加载
- (UIButton *)composeButton {
    if (_composeButton == nil) {
        _composeButton = [[UIButton alloc] init];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_btn"] forState:UIControlStateNormal];
        
        // 设置按钮图片的填充模式
        _composeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        // 设置点击事件
        [_composeButton addTarget:self action:@selector(composeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _composeButton;
}
@end
